package com.applet.applet_shangcheng.controller;

import com.applet.applet_shangcheng.entity.QualityGoods;
import com.applet.applet_shangcheng.service.QualityGoodsService;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.applet.applet_shangcheng.utlis.QueryPageBean;
import com.applet.applet_shangcheng.utlis.Result;
import com.applet.applet_shangcheng.utlis.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/qualityCode")
@CrossOrigin
@Api(value="扫码管理",tags={"扫码管理"})
public class QualityGoodsController {
    @Autowired
    private QualityGoodsService qualityGoodsService;
    @ApiOperation(value="WX_创建扫码Code")
    @PostMapping("/insertCode/{goodsCode}")
    public Result insertQualityGoods(@PathVariable("goodsCode") String goodsCode){
        QualityGoods byCode = qualityGoodsService.findByCode(goodsCode);
        if (byCode!=null){{
            return new Result(false, StatusCode.ERROR, "验证账号也存在!不可申请");
        }}
        int code = qualityGoodsService.insertQualityGoods(goodsCode);
        if (code > 0) {
            return new Result(true, StatusCode.OK, "验证账号添加成功!");
        }
        return new Result(false, StatusCode.ERROR, "验证账号也存在!不可申请");
    };
    @ApiOperation(value="WX_修改扫码Code")
    @PostMapping("/update")
    public Result update(@RequestBody QualityGoods qualityGoods){
        int count = qualityGoodsService.update(qualityGoods);
        if (count > 0) {
            return new Result(true, StatusCode.OK, "验证账号添加成功!");
        }
        return new Result(false, StatusCode.ERROR, "验证账号也存在!不可申请");
    };
    @ApiOperation(value="WX_查询goodsCode扫码")
    @GetMapping("/findByCode/{goodsCode}")
    public  Result findByCode(@PathVariable("goodsCode") String goodsCode){
        QualityGoods qualityGoods = qualityGoodsService.findByCode(goodsCode);
        if (qualityGoods !=null) {
            return new Result(true, StatusCode.OK, "查询成功!",qualityGoods);
        }
        return new Result(false, StatusCode.ERROR, "验证码有误,请重新验证!!!");
    };
    @ApiOperation(value="WX_查询id扫码")
    @GetMapping("/findById/{id}")
    public  Result findById(@PathVariable("id") int id){
        QualityGoods qualityGoods = qualityGoodsService.findById(id);
        if (qualityGoods !=null) {
            return new Result(true, StatusCode.OK, "查询成功!",qualityGoods);
        }
        return new Result(false, StatusCode.ERROR, "验证码有误!!");
    };
    @ApiOperation(value="WX_删除扫码")
    @GetMapping("/deleteCode/{id}")
    public  Result deleteCode(@PathVariable("id") String id){
        int count = qualityGoodsService.deleteCode(id);
        if (count > 0) {
            return new Result(true, StatusCode.OK, "验证账号删除成功!");
        }
        return new Result(false, StatusCode.ERROR, "验证账号删除失败!不可申请");
    };

    //查询所有有效状态
    @ApiOperation(value="_分页查询扫码")
    @PostMapping("/findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = qualityGoodsService.findAll(queryPageBean.getQueryString(),queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        return pageResult;
    }


}
